package cn.rocky.pandacodegen.common.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.concurrent.ConcurrentHashMap;

/**
 * db 枚举类型
 *
 * @author Rocky
 * @date 2023/06/25
 */
@Getter
@AllArgsConstructor
public enum DbType {

    MYSQL(1, "MySQL", "com.mysql.cj.jdbc.Driver",
            "jdbc:mysql://%s:%s/%s?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"
    ),
    ;


    /**
     * 数据库类型
     */
    private final Integer type;

    /**
     * 数据库名字
     */
    private final String dbName;

    /**
     * 数据库驱动
     */
    private final String driverClass;
    /**
     * jdbc url
     */
    private final String jdbcUrl;


    private static final ConcurrentHashMap<Integer, DbType> dbTypeMap;

    static {
        dbTypeMap = new ConcurrentHashMap<>();
        for (DbType dbType : DbType.values()) {
            dbTypeMap.put(dbType.type,dbType);
        }
    }

    /**
     * 获取 DbType
     *
     * @param type 类型
     * @return {@code DbType}
     */
    public static DbType getDbType(Integer type) {
        return dbTypeMap.getOrDefault(type,null);
    }

    public String getJdbcUrl() {
        return jdbcUrl;
    }

    public static Boolean containDbType(Integer type) {
        return dbTypeMap.containsKey(type);
    }



    public static void main(String[] args) {
        System.out.println(DbType.containDbType(1));
        System.out.println(dbTypeMap.get(1));
        System.out.println(DbType.getDbType(1));
    }

}
